﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using Tender.Tool.Server.Model;

namespace Tender.Tool.Server.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    [EnableCors("any")] //允许跨域
    public class UpdateController : ControllerBase
    {
        /// <summary>
        /// 获取服务器版本
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetServerVersion()
        {
            List<short> subDirectories = Directory.GetDirectories($"{AppDomain.CurrentDomain.BaseDirectory}\\File").Select(t=>Convert.ToInt16(Path.GetFileName(t).Replace(".",""))).OrderBy(o=>o).ToList();
            string  number = subDirectories.Last().ToString();
            string part1 = number.Substring(0, 1);
            string part2 = number.Substring(1, 1);
            string part3 = number.Substring(2, 1);
            string part4 = number.Substring(3, 1);
            return $"{part1}.{part2}.{part3}.{part4}";
        }
        /// <summary>
        /// 获取版本信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public VersionInfo GetinVersionformationUir(string Versioninfo)
        {
            var filePath = $"{AppDomain.CurrentDomain.BaseDirectory}\\File\\{Versioninfo}"; // 确保路径正确
            string[] files_ = Directory.GetFiles(filePath, "*", SearchOption.AllDirectories);
            VersionInfo versionInfo = new VersionInfo();
            versionInfo.VersionNumber = Versioninfo;
            versionInfo.FilesNumber = files_.Length;
            foreach (var file in files_)
            {
                versionInfo.Filedownloadaddress.Add(file.Replace(AppDomain.CurrentDomain.BaseDirectory,""));
            }
            return versionInfo;
        }
        /// <summary>
        /// 上传发布文件
        /// </summary>
        /// <param name="formFile"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Uploadfiles([FromForm]IFormFile file, [FromForm]string Versioninfo)
        {
            var filePath = $"{AppDomain.CurrentDomain.BaseDirectory}\\File\\{Versioninfo}\\{file.FileName}"; // 确保路径正确
            if (file == null || file.Length == 0)
            {
                return BadRequest("No file uploaded.");
            }
            if(Directory.Exists($"{AppDomain.CurrentDomain.BaseDirectory}\\File\\{Versioninfo}")==false)
            {
                Directory.CreateDirectory($"{AppDomain.CurrentDomain.BaseDirectory}\\File\\{Versioninfo}");
            }
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            return Ok(new { FilePath = filePath.Replace(AppDomain.CurrentDomain.BaseDirectory, "") });
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="formFile"></param>
        /// <returns></returns>
        //[HttpGet]
        //public IActionResult Downloadfile(string formFile, CancellationToken cancellationToken)
        //{

        //    try
        //    {
        //        cancellationToken.ThrowIfCancellationRequested();
        //        // 检查取消请求

        //        var filePath = $"{AppDomain.CurrentDomain.BaseDirectory}\\{formFile}"; // 确保路径正确
        //        return PhysicalFile(filePath, "application/zip", Path.GetFileName(formFile));
        //    }
        //    catch (TaskCanceledException)
        //    {
        //        // 处理取消请求的情况
        //        return StatusCode(499, "Request was canceled");
        //    }
        //}
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="formFile"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Downloadfile(string formFile)
        {
           var filePath = $"{AppDomain.CurrentDomain.BaseDirectory}\\{formFile}"; // 确保路径正确
           return PhysicalFile(filePath, "application/zip", Path.GetFileName(formFile));
 
        }
    }
}
